﻿namespace LeRenard
{
    /// <summary>
    /// Holds helper/utility methods for the GetHashCode method.
    /// A hashcode is useful for only one thing: putting an object in a hash table. It is by no means a secure way of detecting uniqueness, but it does mean that when a=b, both a and b need to have the same hash.
    /// If in doubt read http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx.
    /// </summary>
    public static class GetHashCodeHelper
    {
        /// <summary>
        /// Creates a combined hashcode for the given objects.
        /// Order of objects put into the hashing method is important.
        /// </summary>
        /// <param name="objects">The objects taken into account when generating a hashcode.</param>
        /// <returns>A hash for the given set of objects.</returns>
        public static int For(params object[] objects)
        {
            var hash = (uint)0;
            var index = 1;

            foreach (var @object in objects)
            {
                hash = (hash + (uint)@object.GetHashCode()) / (uint)index;
                index++;
            }

            return (int)hash;
        }
    }
}